/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog ,datepicker) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                condition: {},
                url: _global.url.api + 'data/ah_gd_activity_goods',
                callback: function() {
                    loaded();
                    console.log(_this.collection.result);
                    $('#sell-view').html(template('sell-view-tpl', {'amount': _this.collection.result.total_amount,'sale_num':_this.collection.result.sale_num}));
                }
            });
            //
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('.long_date').on('click', function() {
                WdatePicker({
                    el:this.id,
                    dateFmt:'yyyy-MM-dd HH:mm:ss'
                });
            });
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.start_time = $("#start_time").val();
                _this.collection.condition.end_time = $("#end_time").val();
                _this.collection.reload();
            });
            //处理默认时间
            var start_time, end_time;
            var curDate = new Date();
            start_time = _this.formatTime(new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), 0, 0, 0));
            end_time = _this.formatTime(new Date());
            $('#start_time').val(start_time);
            $('#end_time').val(end_time);
        },
        formatTime: function (date) {
            var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
            var curDay = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            return date.getFullYear() + '-' + month + '-' + curDay + ' ' + hours + ':' + minutes + ':' + seconds;
        }
    };

    new index();

});